package com.wannengbang.storemobile.merchant.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.bean.RechargeConfigBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class ReConfigListAdapter extends RecyclerView.Adapter<ReConfigListAdapter.ViewHolder> {


    private List<RechargeConfigBean.DataBean> listsBeans;
    private Context context;
    private OnCallBackListener onCallBackListener;

    public ReConfigListAdapter(List<RechargeConfigBean.DataBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_batch_add_recharge_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RechargeConfigBean.DataBean dataBean = listsBeans.get(position);

        holder.tvTitle.setText((position + 1) + ".");
        if (!TextUtils.isEmpty(dataBean.getRecharge_money())) {
            holder.editRechargeMoney.setText(dataBean.getRecharge_money());
        } else {
            holder.editRechargeMoney.setText("");
        }
        if (!TextUtils.isEmpty(dataBean.getSend_money())) {
            holder.editSendMoney.setText(dataBean.getSend_money());
        } else {
            holder.editSendMoney.setText("");
        }

        holder.viewCancel.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.edit_recharge_money)
        EditText editRechargeMoney;
        @BindView(R.id.edit_send_money)
        EditText editSendMoney;
        @BindView(R.id.view_cancel)
        View viewCancel;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            editRechargeMoney.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    listsBeans.get(getAdapterPosition()).setRecharge_money(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            editSendMoney.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    listsBeans.get(getAdapterPosition()).setSend_money(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
